﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Contratos;

namespace welwitschia.Contratos
{
    public partial class ContratosLista : PaginaBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindListView();
        }

        protected void btnCriar_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"~\Contratos\ContratoDetalhe.aspx");
        }

        protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            try
            {
                IContratoService contratoService = new ContratoService();
                contratoService.Apaga(new Contrato() { NumeroContrato = Convert.ToInt64(e.Keys["NumeroContrato"]) });

                BindListView();
            }
            catch (Exception ex)
            {
                MostraErro(ex.Message);
            }
        }

        protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
        {
        }

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
                Response.Redirect(@"~\Contratos\ContratoDetalhe.aspx?nc=" + e.CommandArgument.ToString());
        }

        protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            dplvContratos.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            BindListView();
        }

        #region Metodos auxiliares

        private void BindListView()
        {
            ListView1.DataSource = (new ContratoService()).ObterTodosActivos();
            ListView1.DataBind();
        }

        #endregion

    }
}